package com.dragon.account.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.account.entity.PrCodeTable;
import com.dragon.account.repository.PrCodeTableRepository;
import com.dragon.common.entity.search.Searchable;
import com.dragon.common.repository.mybatis.paginator.domain.PageBounds;
import com.dragon.common.service.BaseService;

/**
 * @author hanchaoyong
 */
@Service
public class PrCodeTableService extends BaseService<PrCodeTable, String> {

    @Autowired
    private PrCodeTableRepository codeTableRepository;

    public Map getGender() {
        Map gender = new HashMap();
        gender.put("0", "男");
        gender.put("1", "女");
        return gender;
    }

    /**
     * 获取代码表
     * @param typeId
     * @return
     */
    public Map<String, String> getCodeTable(String typeId) {
        Map<String, String> map = new LinkedHashMap<String, String>();
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchParam("typeId_eq", typeId);
        searchable.addSearchParam("isvalid_eq", "Y");
        List<PrCodeTable> codeTables = codeTableRepository.selectList(searchable, new PageBounds());
        Iterator<PrCodeTable> iterator = codeTables.iterator();
        while (iterator.hasNext()) {
            PrCodeTable codeTable = iterator.next();
            map.put(codeTable.getId(), codeTable.getName());
        }
        return map;
    }

    /**
     * 获取代码表名称
     * @param typeId
     * @param Id
     * @return
     */
    public String getCodeTableName(String typeId, String Id) {
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchParam("typeId_eq", typeId);
        searchable.addSearchParam("id_eq", Id);
        PrCodeTable codeTable = codeTableRepository.selectBySearchable(searchable);
        if (codeTable != null) {
            return codeTable.getName();
        } else {
            return "";
        }
    }
    
    /**
       * 根据typeId与ID查询PrCodeTable
     * @param typeId
     * @param Id
     * @return
     */
    public PrCodeTable getCodeTable(String typeId, String Id) {
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchParam("typeId_eq", typeId);
        searchable.addSearchParam("id_eq", Id);
        return codeTableRepository.selectBySearchable(searchable);
        
    }
    

    public int selectMaxByBillDate() {
        return codeTableRepository.selectMaxByBillDate();
    }

    /**
     * 获取代码表名称模糊查询
     * @param typeId
     * @param Id
     * @return
     */
    public String getCodeTableNameLike(String typeId, String Id) {
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchParam("typeId_lk", typeId);
        searchable.addSearchParam("id_lk", Id);
        PrCodeTable codeTable = codeTableRepository.selectBySearchable(searchable);
        if (codeTable != null) {
            return codeTable.getName();
        } else {
            return "";
        }
    }
}
